home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / listc.cq / listc.c
Text File  |  1984-04-01  |  8KB  |  259 lines

  1. /**********************************************************
  2.  ***                                                    ***
  3.  ***    Copyright (c) 1981 by David M. Fogg             ***
  4.  ***                                                á  ***
  5.  ***            2632 N.E. Fremont                       ***
  6.  ***            Portland, OR 97212                      ***
  7.  ***                                                    ***
  8.  ***        (503) 288-3502{HM} || 223-8033{WK}          ***
  9.  ***                                                    ***
  10.  ***    Permission is herewith granted for non-         ***
  11.  ***    commercial distribution through the BDS C       ***
  12.  ***    User's Group; any and all forms of commercial   ***
  13.  ***    redistribution are strenuously unwished-for.    ***
  14.  ***                                                    ***
  15.  **********************************************************/
  16.  
  17. /*
  18.           ---> TEXT FILE LISTING PROGRAM <---
  19.  
  20.    Coded by David M. Fogg @ New Day Computing Co., Portland, OR
  21.  
  22.          *** (C) COPYRIGHT 1980, New Day Computing Co. ***
  23.  
  24.    24 OCT 80: creation day
  25.    5 Nov - add optional line numbering
  26.    6 NOV - Chg to xpand tabs
  27.    21 Nov: Filter Form Feeds
  28.    8 Jan 81: add line folding & opt line length spec
  29.    6 Mar: chg DEFLL->128; chg puts->printf 2 show actual DEFLL
  30.    27 mar 1984: extensive revisions to convert this program to 
  31.     Lattice c. and ms.dos. (Pete Mack - Simi Valley RCPM)
  32.  
  33.     USAGE: list filnam "Title String"  {list a single file}
  34.            list [no arguments]   {list multiple files: title/filnam prompts}
  35.                 [no arguments] allows changing:
  36.                                  line numbering   (default: NO)
  37.                                  line length      (default: 70)
  38.                                  initial formfeed (default: NO)
  39.  
  40. */
  41.  
  42. #include "stdio.h"
  43. #include "ctype.h"
  44.  
  45. #define MAXF   64       /* MAX # OF FILES */
  46. #define MAXFNL 15       /* MAX FILNAM LENGTH (INCL NUL TERMINATOR) */
  47. #define MAXLNS 60       /* MAX LINES/PAGE */
  48. #define NSTARS 85       /* # OF ASTERISKS IN STAR LINE */
  49. #define TABX    8       /* TAB EXPANSION MODULUS */
  50. #define DEFLL  128      /* DEFAULT LINE LENGTH */
  51. FILE *LST, *f1;
  52.  
  53. main (argc, argv)
  54. int argc;
  55. char *argv[];
  56. {
  57.    char fnbuf[MAXFNL*MAXF];      /* file name buffer */
  58.    char *pfnbuf;                 /* pointo curr pos in fnbuf */
  59.    char *filnam[MAXF];           /* file names (pointers) */
  60.    char titbuf[100];             /* page title (usu. incl. date) buffer */
  61.    char *titl;                   /* pointer to title */
  62.    char stars[NSTARS+1];         /* asterisk line */
  63.    int fno;                      /* file # being listed */
  64.    int nfils;                    /* no. of files to list */
  65.    int pagno;                    /* curr page # */
  66.    int lino;                     /* curr line # on page */
  67.    char iobuf[_BUFSIZ];          /* file I/O buffer */
  68.    int filopn;                   /* holds fopen result */
  69.    char lbuf[MAXLINE];           /* input line buffer */
  70.    BOOL dilin;                   /* whether to disp line #s */
  71.    int lines;                    /* tot lines in curr file */
  72.    int linlen;                   /* maximum line length */
  73.    char c;
  74.  
  75.  
  76.    if((LST = fopen("PRN", "w")) == NULL){
  77.     cprintf("I can't create PRN");
  78.     exit(1);
  79.    }
  80.  
  81.    pfnbuf = fnbuf;
  82.    titl = titbuf + 2;
  83.    *pfnbuf = (MAXFNL * MAXF) - 2;
  84.    titbuf[0] = 98;
  85.  
  86.    setmem(stars, NSTARS, '*'); stars[NSTARS] = '\0';
  87.    dilin = NO;
  88.    linlen = DEFLL;
  89.  
  90.    if (argc > 1) {
  91.       nfils = 1;
  92.       filnam[0] = *++argv;
  93.       if (argc > 2)
  94.          titl = *++argv;
  95.       else
  96.          titbuf[0] = '\0';
  97.    }
  98.    else {
  99.       dilin = getyn(0, 0, "\nLine Numbers");
  100.       cprintf("\rLine Length (%d): \0", DEFLL);
  101.  
  102.      cgets(lbuf);
  103.      linlen= atoi(lbuf);
  104.      if (linlen < 1)
  105.          linlen = DEFLL;
  106.  
  107.       cputs("\n\rTitle: ");
  108.       cgets(titbuf);
  109.       nfils = 0;
  110.       do {
  111.          cprintf("\nFile # %2d : ", nfils + 1);
  112.          filnam[nfils] = (char *) cgets(pfnbuf);    
  113.          pfnbuf += strlen(pfnbuf) + 1;
  114.       }
  115.     while (strlen(filnam[nfils++]) > 0 && nfils < MAXF);
  116.       --nfils;
  117.       if (nfils == 0)
  118.      {
  119.          fputs("Nothing to do...quitting",stderr);
  120.      exit(1);
  121.      }
  122.       cputs("\nWant a Formfeed? ");
  123.  
  124.       c = getch();
  125.       putch(c);
  126.       if (toupper(c) == 'Y')
  127.      putc(FFEED, LST);
  128.      putchar('\n');
  129.    }
  130.  
  131.  
  132. /*
  133.          >>------> MAIN PRINT LOOP <------<<
  134. */
  135.  
  136.    for (fno = 0; fno < nfils; ++fno) {
  137.     rewind(LST);
  138.     if ((f1 = fopen(filnam[fno], "r")) == NULL) { 
  139.          cprintf("*** File I/O Error: %-15s\n", filnam[fno]);
  140.          continue;
  141.       }
  142.       pagno = 0; lines = 0;
  143.       lino = MAXLNS + 3;
  144.       cprintf("\n\r---> Now listing %-15s\n\r", filnam[fno]);
  145.     while (fgets(lbuf,MAXLINE,f1) != 0) {
  146.          if (lino > MAXLNS) {
  147.             if (pagno == 0) {
  148.                if (fno > 0) putc(FFEED, LST);
  149.                fputs(stars, LST); fputs("\n", LST);
  150.                lino = 1;
  151.             }
  152.             else {
  153.                putc(FFEED, LST);
  154.                lino = 0;
  155.             }
  156.             fprintf(LST, "\n---> Listing of: %-15s    ", filnam[fno]);
  157.             fprintf(LST, "%-35s  ", titl);
  158.             fprintf(LST, "Page %2d <---\n", ++pagno);
  159.             lino += 2;
  160.             if (pagno == 1) {
  161.                fputs("\n", LST); fputs(stars, LST); fputs("\n", LST);
  162.                lino += 2;
  163.             }
  164.             fputs("\n\n\n", LST); lino += 3;
  165.          }
  166.          if (dilin)
  167.             fprintf(LST, "%4d: ", ++lines);
  168.          oplin(lbuf, linlen, &lino);
  169.          ++lino;
  170.       } /* while (fgets... */
  171.  
  172. /*      fclose(iobuf); */
  173.     fclose(f1);
  174.  
  175.    } /* for (fno... */
  176.  
  177.     fputs("\n\r",LST);
  178.       fclose(LST);
  179.  
  180. /*   if (filopn != NULL) putc(FFEED, LST); */
  181.  
  182. } /* main() */
  183.  
  184.  
  185. oplin (lbuf, len, lno)    /* --<O/P A LINE W/ TAB XPANSION>-- */
  186. char lbuf[];
  187. int len;
  188. int *lno;
  189. {
  190.    int cpos;      /* curr printhead pos */
  191.    int i;
  192.  
  193.    cpos = 0;
  194.    for (i = 0; i < strlen(lbuf); ++i) {
  195.       if (lbuf[i] == '\n')
  196.          fputs("\n", LST);
  197.       else if (lbuf[i] == '\t') {
  198.          putc(' ', LST);
  199.          while (++cpos % TABX) putc(' ', LST);
  200.       }
  201.       else if (lbuf[i] != FFEED) {
  202.          putc(lbuf[i], LST);
  203.          if (++cpos > len) {
  204.             fputs("\\\n", LST);
  205.             cpos = 0;
  206.             ++*lno;
  207.          }
  208.       }
  209.    }
  210. }
  211. /*------------------------------------------------------------*/
  212. /* This function was originally designed to get an answer
  213.    'yes' or 'no' at a specific xy coordinate..
  214.     there is a current problem in that toupper doesnt appear
  215.     to operate properly if we use the statement...
  216.        ans = toupper(getch());
  217.     consequently we have temporarily set up this scheme...(pm) */
  218.  
  219. BOOL getyn (x, y, prom)         /* --<PROMPT 4 Y/N ANSWER>-- */
  220. int x,y;
  221. char *prom;
  222. {
  223.    char ans;
  224.    char anst;
  225.  
  226. /*   if (x != 0) toxy(x, y);    */ /* set x=0 2 skip cursor pos */
  227.  
  228.    cprintf("%s (Y/N)? ", prom);
  229.  
  230.    do {
  231.       anst = getch() & 0x7f;
  232.       ans = toupper(anst);
  233.    } while (ans != 'Y' && ans != 'N');
  234.  
  235.    putch(ans); putch('\n');
  236.  
  237.    return (ans == 'Y' ? YES : NO);
  238. }
  239. /************************************************
  240.  atoi function from K&R pagew 58. 
  241.  atoi -- convert s to integer --
  242.  rev 3-12-84 initial entry
  243. *************************************************/
  244. int atoi(s)
  245. char s[];
  246. {
  247.     int i, n, sign;
  248.  
  249.     for( i=0; s[i] == ' ' || s[i] == '\n' || s[i] == '\t'; i++)
  250.         ; /* skip white space */
  251.     sign = 1;
  252.     if (s[i] == '+' || s[i] == '-')
  253.         sign = (s[i++] == '+') ? 1  : -1;
  254.  
  255.     for (n = 0; s[i] >= '0' && s[i] <= '9' ; i++)
  256.         n = 10 * n + s[i] - '0';
  257.     return(sign * n);
  258. }
  259.